﻿#include "ElementTreeModel.h"
#include "ElementTreeItem.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <qpainter.h>

///////////////////////////////////////////////////////////////////////////
/// ElementTreeModel
///////////////////////////////////////////////////////////////////////////
ElementTreeModel::ElementTreeModel(QObject *parent)
    : QAbstractItemModel{parent}
{
}

ElementTreeModel::~ElementTreeModel()
{

}

int ElementTreeModel::columnCount(const QModelIndex &parent) const
{
    if(!parent.isValid())
        return rootItem->valuesCount();
    return (
               (ElementTreeItem*)parent.internalPointer()
            )->valuesCount();
}

int ElementTreeModel::rowCount(const QModelIndex &parent) const
{
    if (parent.column() > 0)
        return 0;
    if(!parent.isValid())
        return rootItem->childrenCount();
    return (
               (ElementTreeItem*)parent.internalPointer()
               )->childrenCount();
}
/*
    DisplayRole = 0,// 显示的文字
    DecorationRole = 1,// 图标,QIcon
    EditRole = 2, //
    ToolTipRole = 3, // 提示
    StatusTipRole = 4, // 状态栏信息
    WhatsThisRole = 5,
    // Metadata
    FontRole = 6, // 显示的文字字体
    TextAlignmentRole = 7, // 显示的文字对其方式
    BackgroundRole = 8,// 背景
    ForegroundRole = 9,
    CheckStateRole = 10,
    // Accessibility
    AccessibleTextRole = 11,
    AccessibleDescriptionRole = 12,
    // More general purpose
    SizeHintRole = 13,
    InitialSortOrderRole = 14,
    // Internal UiLib roles. Start worrying when public roles go that high.
    DisplayPropertyRole = 27,
    DecorationPropertyRole = 28,
    ToolTipPropertyRole = 29,
    StatusTipPropertyRole = 30,
    WhatsThisPropertyRole = 31,
    // Reserved
    UserRole = 0x0100
*/
QVariant ElementTreeModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();
    ElementTreeItem* item=(ElementTreeItem*)index.internalPointer();
    if(item&&item->have(role)){
        return item->value(role);
    }
    if(role==Qt::ItemDataRole::SizeHintRole){
        return QSize(0,25);
    }
    if(role==Qt::ItemDataRole::StatusTipRole)
        return item->value(Qt::ItemDataRole::DisplayRole);
    return QVariant();
}

QModelIndex ElementTreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if(!hasIndex(row,column,parent)){
        return QModelIndex();
    }
    ElementTreeItem* p=nullptr;
    if(!parent.isValid()){
        p = rootItem;
    }else{
        p = (ElementTreeItem*)parent.internalPointer();
    }
    if(
        row>=0&&row<p->childrenCount()
        )
    {
        ElementTreeItem* child = p->child(row);
        if(child)
            return QAbstractItemModel::createIndex(row,column,child);
    }
    return QModelIndex();
}

QModelIndex ElementTreeModel::parent(const QModelIndex &index) const
{
    if(!index.isValid()){
        return QModelIndex();
    }
    ElementTreeItem* element = (ElementTreeItem*)index.internalPointer();
    ElementTreeItem* parentItem = element->parent();
    if(parentItem==rootItem){
        return QModelIndex();
    }
    return createIndex(parentItem->rowOnParent(), 0, parentItem);
}
///////////////////////////////////////////////////////////////////////////
/// ReadElements
///////////////////////////////////////////////////////////////////////////
/*
    DisplayRole = 0,// 显示的文字
    DecorationRole = 1,// 图标
    EditRole = 2, //
    ToolTipRole = 3, // 提示
    StatusTipRole = 4, // 状态栏信息
    WhatsThisRole = 5,
    // Metadata
    FontRole = 6, // 显示的文字字体
    TextAlignmentRole = 7, // 显示的文字对其方式
    BackgroundRole = 8,// 背景
*/
/*
    DisplayRole = 0,// 显示的文字
    DecorationRole = 1,// 图标,QIcon
    EditRole = 2, //
    ToolTipRole = 3, // 提示
    StatusTipRole = 4, // 状态栏信息
    WhatsThisRole = 5,
    // Metadata
    FontRole = 6, // 显示的文字字体
    TextAlignmentRole = 7, // 显示的文字对其方式
    BackgroundRole = 8,// 背景
    ForegroundRole = 9,
    CheckStateRole = 10,
    // Accessibility
    AccessibleTextRole = 11,
    AccessibleDescriptionRole = 12,
    // More general purpose
    SizeHintRole = 13,
    InitialSortOrderRole = 14,
    // Internal UiLib roles. Start worrying when public roles go that high.
    DisplayPropertyRole = 27,
    DecorationPropertyRole = 28,
    ToolTipPropertyRole = 29,
    StatusTipPropertyRole = 30,
    WhatsThisPropertyRole = 31,
    // Reserved
    UserRole = 0x0100
*/

// ElementTreeItem *ElementTreeModel::ReadElements::read(const QString &filePath)
// {
//     QFile file(filePath);
//     file.open(QIODeviceBase::ReadOnly);
//     if(!file.isOpen()){
//         return nullptr;
//     }
//     QByteArray json=file.readAll();
//     QJsonDocument doc=QJsonDocument::fromJson(json);
//     if(!doc.isObject()){
//         return nullptr;
//     }
//     QJsonObject obj=doc.object();
//     QStringList keys=obj.keys();
//     ElementTreeItem *pFirstItem = new ElementTreeItem();
//     pFirstItem->setValue(Qt::DisplayRole,"元件");
//     for(int i=0;i<keys.length();++i){
//         ElementTreeItem *itemi = new ElementTreeItem();
//         itemi->setValue(Qt::DisplayRole,keys[i]);
//         pFirstItem->appendChild(itemi);
//         QJsonObject temObj = obj[keys[i]].toObject();
//         itemi->setValue(Qt::DecorationRole,QIcon(temObj["icon"].toString()));
//         QJsonArray temArray = temObj["elements"].toArray();
//         for(int j=0;j<temArray.count();++j){
//             ElementTreeItem *itemj = new ElementTreeItem();
//             QJsonObject tem = temArray[j].toObject();
//             itemj->setValue(Qt::DisplayRole,tem["title"].toVariant());
//             QString iconPth = tem["icon"].toString();
//             if(iconPth.isEmpty())
//                 itemj->setValue(Qt::DecorationRole,QIcon("://icon/Electrical automation.svg"));
//             else
//                 itemj->setValue(Qt::DecorationRole,QIcon(iconPth));
//             itemi->appendChild(itemj);
//         }
//     }
//     return pFirstItem;
// }
